package com.weishuang.monitor.leader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.weishuang.monitor.leader.commons.utils.FormatUtil;
import com.weishuang.monitor.leader.entity.ServerRamRealTime;
import com.weishuang.monitor.leader.entity.vo.ServerRamRealVo;
import com.weishuang.monitor.leader.mapper.ServerRamRealTimeMapper;
import com.weishuang.monitor.leader.mapper.ServersBaseInfoMapper;
import com.weishuang.monitor.leader.service.IServerRamRealTimeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务器内存实时信息 服务实现类
 * </p>
 *
 * @author weishuang
 * @since 2021-03-23
 */
@Service
public class ServerRamRealTimeServiceImpl extends ServiceImpl<ServerRamRealTimeMapper, ServerRamRealTime> implements IServerRamRealTimeService {

    @Value("${leader.limitNum.ram:100000}")
    private Long limitNumRam;

    @Autowired
    private ServersBaseInfoMapper serversBaseInfoMapper;

    /**
     * 服务器实时内存限制处理
     */
    @Override
    public void serverRamDataLimit() {
        List<String> serversId = serversBaseInfoMapper.listNormalAndOnlieServer();
        if (serversId != null && serversId.size() > 0) {
            for (String serverId : serversId) {
                baseMapper.deleteLimitNumData(serverId,limitNumRam);
            }
        }
    }

    /**
     * 获取最新150条Ram数据
     * @param sysId
     * @return
     */
    @Override
    public List<ServerRamRealVo> listTop(String sysId) {
        LambdaQueryWrapper<ServerRamRealTime> first = Wrappers.lambdaQuery(ServerRamRealTime.class).eq(ServerRamRealTime::getSysId, sysId)
                .orderByDesc(ServerRamRealTime::getCreatedTime).last("limit 100").orderByAsc(ServerRamRealTime::getCreatedTime);
        List<ServerRamRealVo> serverRamRealVos = new ArrayList<>();
        List<ServerRamRealTime> serverRamRealTimes = baseMapper.selectList(first);
        for (ServerRamRealTime serverRamRealTime : serverRamRealTimes) {
            ServerRamRealVo serverRamRealVo = new ServerRamRealVo();
            serverRamRealVo.setTotal(FormatUtil.formatBytes(serverRamRealTime.getTotal()));
            serverRamRealVo.setAvailable(FormatUtil.formatBytes(serverRamRealTime.getAvailable()));
            serverRamRealVo.setUse(FormatUtil.formatBytes(serverRamRealTime.getTotal() - serverRamRealTime.getAvailable()));
            BigDecimal multiply = new BigDecimal(String.valueOf(serverRamRealTime.getTotal() - serverRamRealTime.getAvailable())).divide(new BigDecimal(String.valueOf(serverRamRealTime.getTotal())), 4, BigDecimal.ROUND_DOWN).multiply(new BigDecimal("100"));
            serverRamRealVo.setUseRate(multiply);
            serverRamRealVo.setCreatedTime(serverRamRealTime.getCreatedTime());
            serverRamRealVos.add(serverRamRealVo);
        }
        return serverRamRealVos;
    }

}
